﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using BusinessObjects;

namespace DataAccess.SqlServer
{
    public class SqlServerNamHoc: INamHoc
    {
        public NamHoc GetNamHoc(int oid)
        {
            string sql = @" Select TuNam, DenNam, TenNamHoc 
                            From NamHoc
                            Where Oid = @oid";
            SqlParameter param = new SqlParameter("@oid", oid);
            DataRow row = DataProvider.GetDataRow(sql, param);

            short tuNam = Int16.Parse(row["TuNam"].ToString());
            short denNam = Int16.Parse(row["DenNam"].ToString());
            string tenNamHoc = row["TenNamHoc"].ToString();

            NamHoc namhoc = new NamHoc()
            {
                Oid = oid,
                TuNam = tuNam,
                DenNam = denNam,
                TenNamHoc = tenNamHoc
            };
            return namhoc;
        }

        public List<NamHoc> GetListNamHoc()
        {
            string sql = @" Select Oid, TuNam, DenNam, TenNamHoc 
                            From NamHoc";

            DataTable dt = DataProvider.GetDataTable(sql);
            List<NamHoc> list = new List<NamHoc>();

            foreach (DataRow row in dt.Rows)
            {
                int oid = int.Parse(row["Oid"].ToString());
                short tuNam = Int16.Parse(row["TuNam"].ToString());
                short denNam = Int16.Parse(row["DenNam"].ToString());
                string tenNamHoc = row["TenNamHoc"].ToString();

                NamHoc namhoc = new NamHoc()
                {
                    Oid = oid,
                    TuNam = tuNam,
                    DenNam = denNam,
                    TenNamHoc = tenNamHoc
                };
                list.Add(namhoc);
            }
            return list;
        }

        public int Insert(NamHoc namHoc)
        {
            string sql = @" Insert into NamHoc(TuNam, DenNam, TenNamHoc)
                            Values(@tuNam, @denNam, @tenNamHoc)";

            SqlParameter[] param = 
            {
                new SqlParameter("@tuNam", namHoc.TuNam),
                new SqlParameter("@denNam", namHoc.DenNam),
                new SqlParameter("@tenNamHoc", namHoc.TenNamHoc)
            };
            int id = DataProvider.Insert(sql, true, param);
            namHoc.Oid = id;
            return id;
        }

        public int Update(NamHoc namHoc)
        {
            string sql = @" Update NamHoc
                            Set     TuNam = @tuNam,
                                    DenNam = @denNam,
                                    TenNamHoc = @tenNamHoc
                            Where Oid = @oid";

            SqlParameter[] param = 
            {
                new SqlParameter("@tuNam", namHoc.TuNam),
                new SqlParameter("@denNam", namHoc.DenNam),
                new SqlParameter("@tenNamHoc", namHoc.TenNamHoc),
                new SqlParameter("@oid", namHoc.Oid)
            };

            DataProvider.Update(sql, param);
            return 0;
        }

        public int Delete(NamHoc namHoc)
        {
            string sql = @" Delete from NamHoc Where Oid = @oid";
            SqlParameter param = new SqlParameter("@oid", namHoc.Oid);

            return DataProvider.Update(sql, param);
        }


        public int Save(NamHoc obj)
        {
            throw new NotImplementedException();
        }
    }
}
